﻿Imports System.Runtime.CompilerServices
Imports System.Text.RegularExpressions
Imports System.Globalization
Imports System.Web


Public Module StringExtension

    'Created by : Sum
    'Created Date : 28/12/2010
    'description : use to remove array gap
    <Extension()> _
Public Function RemoveGap(ByVal pArray() As String) As String()
        Dim CountGap As Integer = 0
        For Each i In pArray
            i = i.Trim()
            If String.IsNullOrEmpty(i) Then
                CountGap += 1
            End If
        Next
        Dim pArray2(pArray.Length - CountGap - 1) As String

        Dim index As Integer = 0
        For Each i In pArray
            i = i.Trim()
            If Not String.IsNullOrEmpty(i) Then
                pArray2(index) = i
                index += 1
            End If
        Next

        Return pArray2
    End Function


    'validation
    <Extension()> _
  Public Function CheckICWithDash(ByVal str As String) As Boolean
        Dim expr = "\d{6}-\d{2}-\d{4}"
        Dim reg As New Regex(expr)
        Return reg.IsMatch(str)
    End Function

    <Extension()> _
  Public Function CheckICWithoutDash(ByVal str As String) As Boolean
        Dim expr = "\d{12}"
        Dim reg As New Regex(expr)
        Return reg.IsMatch(str)
    End Function

    <Extension()> _
   Public Function CheckOldICFormat(ByVal str As String) As Boolean

        Dim expr = "[A-Za-z]{1}\d{9}"
        Dim reg As New Regex(expr)
        Return reg.IsMatch(str)
    End Function

    <Extension()> _
  Public Function CheckIsWord(ByVal str As String) As Boolean
        Dim rev = checkDigit(str)
        If rev = True Then
            Return False
        End If

        Dim expr = "[A-Za-z]"
        Dim reg As New Regex(expr)
        Return reg.IsMatch(str)
    End Function

    <Extension()> _
   Public Function checkDigit(ByVal str As String) As Boolean
        Dim expr = "\d"
        Dim reg As New Regex(expr)
        Return reg.IsMatch(str)
    End Function



    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="str">string</param>
    ''' <param name="datePattern">ex.dd/MM/yyyy</param>
    ''' <param name="defaultDateTime">if fail to convert, return this value</param>
    ''' <returns>'returns the given string to valid datetime, if fail to convert return _default Datetime</returns>
    ''' <remarks></remarks>
    <Extension()> _
    Public Function ToDateTime(ByVal str As String, ByVal datePattern As String, ByVal defaultDateTime As DateTime) As DateTime
        str = str.Trim()
        Dim revDate As DateTime = defaultDateTime
        If DateTime.TryParseExact(str, datePattern, Nothing, DateTimeStyles.None, revDate) Then
            Return revDate
        Else
            Return defaultDateTime
        End If
    End Function

    'Returns the given string truncated to the specified length, suffixed with the given value
    <Extension()> _
    Public Function Truncate(ByVal input As String, ByVal length As Integer, ByVal suffix As String) As String
        If String.IsNullOrEmpty(input) Then
            Return ""
        End If
        If input.Length <= length Then
            Return input
        End If
        If (suffix Is Nothing) Then
            suffix = "..."
        End If
        Return (input.Substring(0, (length - suffix.Length)) + suffix)
    End Function

    <Extension()> _
    Public Function Truncate(ByVal input As String, ByVal length As Integer) As String
        Return Truncate(input, length, "...")
    End Function

    'Splits a given string into an array based on character line breaks
    <Extension()> _
    Public Function ToLineArray(ByVal input As String) As String()
        If String.IsNullOrEmpty(input) Then
            Return New String(-1) {}
        End If
        Return System.Text.RegularExpressions.Regex.Split(input, "" & vbCrLf)
    End Function

    'Splits a given string into a strongly-typed list based on character line breaks
    <Extension()> _
    Public Function ToLineList(ByVal input As String) As List(Of String)
        Dim output As List(Of String) = New List(Of String)
        output.AddRange(input.ToLineArray)
        Return output
    End Function

    'Replaces line breaks with self-closing HTML 'br' tags
    <Extension()> _
    Public Function ReplaceBreaksWithBR(ByVal input As String) As String
        Return String.Join("<br/>", input.ToLineArray)
    End Function


    <Extension()> _
    Public Function DoubleApostrophes(ByVal input As String) As String
        Return Regex.Replace(input, "'", """")
    End Function

    'Encodes the input string as HTML (converts special characters to entities)
    <Extension()> _
    Public Function ToHTMLEncoded(ByVal input As String) As String
        Return HttpContext.Current.Server.HtmlEncode(input)
    End Function

    ' Encodes the input string as a URL (converts special characters to % codes)
    <Extension()> _
    Public Function ToURLEncoded(ByVal input As String) As String
        Return HttpContext.Current.Server.UrlEncode(input)
    End Function

    <Extension()> _
    Public Function StripHTML(ByVal input As String) As String
        Return Regex.Replace(input, "<(style|script)[^<>]*>.*?</\1>|</?[a-z][a-z0-9]*[^<>]*>|<!--.*?-->", "")
    End Function

    <Extension()> _
    Public Function RemoveExtraNewLine(ByVal sText As String) As String
        Dim sPattern, sReplaceText As String
        sPattern = "\r\n"
        sReplaceText = String.Empty
        Return Regex.Replace(sText, sPattern, sReplaceText)
    End Function

    <Extension()> _
    Public Function IsNullOrWhitespace(ByVal s As String) As Boolean
        Dim rev As Boolean = False
        If s Is Nothing Then
            Return True
        End If


        For i = 0 To s.Length - 1
            If Char.IsWhiteSpace(s, i) Then
                rev = True
                Return rev
            End If
        Next


        Return rev
    End Function

    <Extension()> _
    Public Function IsUnicode(ByVal s As String) As Boolean

        Dim items As Char() = s.ToCharArray()
        For i As Integer = 0 To items.Length - 1

            If CUShort(AscW(items(i))) > 255 Then
                Return True
            End If

        Next

        Return False

    End Function

    <Extension()> _
   Public Function get_BetweenList(ByVal InputText As String, ByVal starttext As String, ByVal endtext As String) As List(Of String)
        Dim _list As New List(Of String)

        While 1
            Dim rev = Get_Between(InputText, starttext, endtext)
            _list.Add(rev)
            InputText = InputText.Replace(starttext & rev & endtext, "")
            If Not InputText.Contains(starttext) And Not InputText.Contains(endtext) Then
                Exit While
            End If
        End While
        Return _list
    End Function
    <Extension()> _
    Public Function Get_Between(ByVal InputText As String, ByVal starttext As String, ByVal endtext As String) As String

        Dim lnTextStart As Long
        Dim lnTextEnd As Long

        Dim rev As String = ""


        lnTextStart = InStr(1, InputText, starttext, vbTextCompare) + Len(starttext)
        lnTextEnd = InStr(lnTextStart, InputText, endtext, vbTextCompare)

        If lnTextStart >= (1 + Len(starttext)) And lnTextEnd > lnTextStart Then
            rev = Mid$(InputText, lnTextStart, lnTextEnd - lnTextStart)

        Else
            rev = "ERROR"
        End If

        Return rev
    End Function


End Module
